<?php
/** Zend_Controller_Action */ 
require_once APPLICATION_PATH . '/../library/Soooner/Controller/Action.php';
class VodController extends Soooner_Controller_Action
{
	private $_vid;
	private $_modelVod;
	private $_modelTypes;
	private $_modelSlaves;
	private $_modelLvsvip;
    public function init()
    {
        /* Initialize action controller here */
    	$this->initDzTemplate();
    	$this->_modelVod = new Model_Tables_Vod();
    	$this->_modelTypes = new Model_Tables_Types();
    	$this->_modelSlaves = new Model_Tables_Slaves();
    	$this->_modelLvsvip = new Model_Tables_Lvsvip();
    }
    /**
    * 获取点播列表
    * @return unknown_type
    */
    public function indexAction()
    {
    	/*$options = $GLOBALS['application']->getOptions();
    	$numPerPage = $options["pagecount"];//每页显示的条数
        $ItemCount =  $this->_modelVod->getCount();
        $PageCount = ceil($ItemCount/$numPerPage);
        $PageCount = $PageCount ? $PageCount : 1;*/
		include $this->_dzTemplate->getFile( 'vod.manage.html' );
    }
    /**
    * 获取点播列表
    * @return unknown_type
    */
    public function indexajaxAction()
    {
    	$types=$this->_modelTypes->getTypesList("vod");
        $options = $GLOBALS['application']->getOptions();
    	$numPerPage = $options["pagecount"];//每页显示的条数
        $page = intval($this->_getParam('page'));//默认页码
        $page = empty($page)?'1':$page;
   		$s = $this->_getParam('s'); 
        $ItemCount =  $this->_modelVod->getCount($s);
        $PageCount = ceil($ItemCount/$numPerPage);
        if($page>$PageCount)$page=$PageCount;
        $infos0 = $this->_modelVod->getList( $page, $numPerPage, $s );
        foreach($infos0 as $info){
        	$info["type"]=$types[$info["type"]];
        	$infos[]=$info;
        }
        include $this->_dzTemplate->getFile( 'ajax/vod.manage.ajax.html' );
    }
    /**
    * 添加点播
    * @return unknown_type
    */
    public function addAction()
    {
    	$webheader = rtrim($this->_configs["web"],"/");
    	$types=$this->_modelTypes->getTypesList("vod");  
    	
    	$pubmod=$this->_configs["pubmod"];
    	if($pubmod=="MULTY"){
    		//多机模式
    		$slaves=$this->_modelLvsvip->getVips("REALVOD");
    		$slaveid = 0;
    	}else{
	    	//单机模式
	    	$slaves=$this->_modelSlaves->getSlavesList();
    		$slaveid=$this->_modelSlaves->getSlaveId($this->_configs["myip"]);
    	}

    	$request = $this->getRequest();        
        if ($request->isPost())
        {
            $params =$request->getPost();
            $params["tag1"]=implode(",",$params["tag1"]);
            $params["tag2"]=implode(",",$params["tag2"]);
            $params["tag3"]=implode(",",$params["tag3"]);
            $this->_modelVod->add($params);                
            $this->_redirect("/vod/index/");
        } 
        include $this->_dzTemplate->getFile( 'vod.add.html' );
    }
    /**
    * 修改点播
    * @return unknown_type
    */
    public function modifyAction()
    {
    	$webheader = rtrim($this->_configs["web"],"/");
    	$types=$this->_modelTypes->getTypesList("vod");  
    	$vid = $this->_getParam('vid');
    	$info = $this->_modelVod->getInfo($vid); 
    	
    	$pubmod=$this->_configs["pubmod"];
    	if($pubmod=="MULTY"){
    		//多机模式
    		$slaves=$this->_modelLvsvip->getVips("REALVOD");
    	}else{
	    	//单机模式
	    	$slaves=$this->_modelSlaves->getSlavesList();
    	}
    	
        $request = $this->getRequest();        
        if ($request->isPost())
        {
        	$params =$request->getPost(); 
        	$params["tag1"]=implode(",",$params["tag1"]);
            $params["tag2"]=implode(",",$params["tag2"]);
            $params["tag3"]=implode(",",$params["tag3"]);          	
            if (true)//服务端检查输入参数
            {
                $this->_modelVod->modify($params,$vid);
                $this->_redirect("/vod/");
            }
        } 
        include $this->_dzTemplate->getFile( 'vod.edit.html' );
    }
    /**
    * 无刷新修改点播
    * @return unknown_type
    */    
    public function modifyajaxAction()
    {
        $request = $this->getRequest();
        $params = $request->getPost();
        $arr = split("\|", $params['id']);
        $field = $arr[0];
        $vid = $arr[1];
        $value = stripslashes($params['value']);
        $this->_modelVod->modifyAjax($vid,$field,$value);
        echo $value;
        exit;
    }
    public function modifyajaxreversAction()
    {
    	$field = $this->_getParam('field');
    	$pri = $this->_getParam('pri');
        $this->_modelVod->modifyAjaxRevers($field,$pri);
        exit;
    }
    /**
    * 删除点播
    */
    public function deleteAction()
    {
        $vid = $this->_getParam('vid');   
        $this->_modelVod->del($vid); 
        exit;
    }
    
    public function tagajaxAction()
    {
    	$typeid = intval($this->_getParam('typeid'));
    	$vid = $this->_getParam('vid');
    	$info = $this->_modelVod->getInfo($vid); 
    	$tags=$this->_modelTypes->getTags($typeid);
   		include $this->_dzTemplate->getFile( 'ajax/types.tags.ajax.html' );
    }
}
